//
//  JJVisitorView.swift
//  Weibo
//
//  Created by Adobe on 15/9/27.
//  Copyright © 2015年 龚巨军. All rights reserved.
//

import UIKit

import SnapKit

@objc
protocol JJVisitorViewDelegate:NSObjectProtocol
{
    optional func visitorViewDidRegisterBtnClick(visitorView:JJVisitorView)
    
    optional func visitorViewLoginBtnClick(visitorView:JJVisitorView)
}
class JJVisitorView: UIView {

    
    weak var delegate:JJVisitorViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        self.backgroundColor = UIColor(red: 232.0/255.0, green: 233.0/255.0, blue: 232.0/255.0, alpha: 1.0)
        
        setupUI()
        

    }
    
    func setupVisitorInfo(isHome: Bool, imageName: String, info:String)
    {
        // 1.根据不同的控制器设置不同的数据
        icon.hidden = !isHome
        homeIcon.image = UIImage(named: imageName)
        message.text = info
        
        // 2.判断是否是首页,如果是就执行动画
        if isHome
        {
            startAnimator()
        }
    }
    
    
    func registerClick()
    {
        
        delegate?.visitorViewDidRegisterBtnClick!(self)
    }
    
    func loginClick()
    {
        delegate?.visitorViewLoginBtnClick!(self)
    }
    
    
    
    
    //MARK: - 执行动画
    private func startAnimator()
    {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.duration = 20.0
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false
        icon.layer.addAnimation(anim, forKey: nil)
    }
    private func setupUI()
    {
        addSubview(icon)
        addSubview(bg)
        addSubview(homeIcon)
        addSubview(message)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //MARK: - 布局子控件
        
        icon.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        homeIcon.snp_makeConstraints { (make) -> Void in
           make.center.equalTo(self)
        }
        
        message.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(icon.snp_bottom)
            make.width.equalTo(224)
            make.centerX.equalTo(self)
            
        }
        
        registerButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(message.snp_bottom).offset(20)
            make.left.equalTo(message.snp_left)
            make.size.equalTo(CGSize(width: 100, height: 35))
            
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(message.snp_bottom).offset(20)
            make.right.equalTo(message.snp_right)
            make.size.equalTo(CGSize(width: 100, height: 35))
            
        }
        
        bg.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(0)
        }
        
    }

    private lazy var icon :UIImageView = {
      let icon = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        return icon
    
    }()
 
    private lazy var homeIcon :UIImageView = {
        let homeIcon = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        
        return homeIcon
        
        }()
    private lazy var message :UILabel = {
       let label = UILabel()
        
        label.text = "附近的思考了几分离开的时间发来看电视剧罚款了但是但是"
        
        label.numberOfLines = 0
        
        return label
        
    }()
    
    
    private lazy var registerButton :UIButton = {
        
        let btn = UIButton()
        
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        // 注册按钮点击监听
        btn.addTarget(self, action: Selector("registerClick"), forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
        
    }()
    
    private lazy var loginButton :UIButton = {
    
        let btn = UIButton()
        btn.setTitle("登录", forState: UIControlState.Normal)
        
        btn.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
    

        btn.addTarget(self, action: Selector("loginClick"), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    
    private lazy var bg :UIImageView = {
       let bg = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        
        return bg
    }()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
